<?php
/**
 * Created by jinqiang on 2024/7/4 11:15.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Services\Produce;


use App\Http\Models\Mysql\SsyProcessingListBatchTable;

class BatchService
{
    public function search($param)
    {
        $conn = new SsyProcessingListBatchTable();
        //状态
        if(isset($param['status']) && $param['status']!=""){
            $conn = $conn->where('status', $param['status']);
        }
        if($param['batch_title'] ?? ""){
            $conn = $conn->where('batch_title', $param['batch_title']);
        }
        if($param['batch_date'] ?? ""){
            $conn = $conn->where('batch_date', $param['batch_date']);
        }
        $res = $conn->orderBy('batch_date', 'desc')->paginate((Int)$param['limit'])->toArray();
        return [
            'data' => $res['data'],
            'total' => $res['total']
        ];
    }

    public function setStatus($id, $status)
    {
        return (new SsyProcessingListBatchTable())->updateData((Int)$id, ['status' => (Int)$status]);
    }

    public function save($param)
    {
        $id = $param['id'] ?? "";
        $data = [
            "batch_title" => $param['batch_title'],
            "batch_date" => $param['batch_date'],
            "batch_no" => date("Ymd", strtotime($param['batch_date'])),
            "createtime" => date("Y-m-d H:i:s")
        ];
        if(!empty($id)){
            unset($data['createtime']);
            return (new SsyProcessingListBatchTable())->updateData($id, $data);
        }else{
            return (new SsyProcessingListBatchTable())->createData($data);
        }
    }
}
